//
//  EHRegisterViewController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/12.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import UIKit
import SwiftFontName
import RxSwift

public class EHRegisterViewController: UIViewController {
    let bag = DisposeBag()

    @IBOutlet weak var phoneNumberTF: UITextField! {
        didSet {
            phoneNumberTF.tintColor = UIColor("#00C8C2")
            phoneNumberTF.keyboardType = .phonePad
        }
    }
    @IBOutlet weak var verifyCodeTF: UITextField! {
        didSet {
            verifyCodeTF.tintColor = UIColor("#00C8C2")
            phoneNumberTF.keyboardType = .decimalPad
        }
    }
    @IBOutlet weak var loginButton: UIButton! {
        didSet {
            loginButton.layer.cornerRadius = 8
            loginButton.layer.masksToBounds = true
            loginButton.isEnabled = false
            loginButton.setTitleColor(.lightGray, for: .disabled)
        }
    }
    @IBOutlet weak var fetchCaptchaButton: UIButton! {
        didSet {
            fetchCaptchaButton.isEnabled = false
        }
    }

    public override func viewDidLoad() {
        super.viewDidLoad()
        bindUI()
    }

    @IBAction func fetchVerificationCode(_ sender: UIButton) {
        guard let text = phoneNumberTF.text else { assertionFailure("unexpected happened"); return }
        FetchCaptchaViewModel()
            .fetch(phoneNo: text, scene: "1", disposeBag: bag)
    }

    @IBAction func login(_ sender: UIButton) {
        guard let phoneNo = phoneNumberTF.text,
            let captcha = verifyCodeTF.text else { EHToast.show("手机号或验证码不正确"); return }
        LoginViewModel()
            .logIn(phoneNo: phoneNo, captcha: captcha, bag: bag)
    }

    private func bindUI() {
        phoneNumberTF.rx.controlEvent(.editingChanged)
            .subscribe(onNext: { [unowned self] in
                if let text = self.phoneNumberTF.text {
                    self.phoneNumberTF.text = String(text.prefix(11))
                }
            }).disposed(by: bag)

        let phoneNumberValid = phoneNumberTF.rx.text.orEmpty
            .map { $0.count == 11 }
            .share(replay: 1, scope: .whileConnected)
        phoneNumberValid
            .bind(to: fetchCaptchaButton.rx.isEnabled)
            .disposed(by: bag)

        verifyCodeTF.rx.controlEvent(.editingChanged)
            .subscribe(onNext: { [unowned self] in
                if let text = self.verifyCodeTF.text {
                    self.verifyCodeTF.text = String(text.prefix(6))
                }
            }).disposed(by: bag)

        let verfityCodeValid = verifyCodeTF.rx.text.orEmpty
            .map { $0.count == 6 }
            .share(replay: 1, scope: .whileConnected)

        let everythingValid = Observable.combineLatest(phoneNumberValid, verfityCodeValid) { $0 && $1 }
            .share(replay: 1, scope: .whileConnected)

        everythingValid
            .bind(to: loginButton.rx.isEnabled)
            .disposed(by: bag)
    }
}
